<template>
  <!--学习路径 编辑-->
  <div class="app-container">
    <!--返回上一步-->
    <div class="back-bar-box mb-30">
      <div class="btn-back" @click="$router.go(-1)"><i class="el-icon-back"></i> 返回</div>
      <div class="line"></div>
      <div class="name">{{$route.name}}</div>
    </div>
    <!--表单-->
    <el-form label-width="120px" :model="path" :rules="rules" ref="path">
      <el-form-item label="分类" prop="categoryId">
        <el-select
          v-model="path.categoryId"
          filterable
          placeholder="请选择">
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>

        <el-link @click="$router.push('/learningpath/category')" type="primary">分类管理</el-link>

      </el-form-item>

      <el-form-item label="标题" prop="name">
        <el-input v-model="path.name" style="width: 400px"/>
      </el-form-item>

      <el-form-item label="封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_URL+'/eduoss/fileoss?host=path'"
          class="avatar-uploader">
          <img v-if="path.cover" :src="path.cover" style="width: 200px">
          <el-button type="primary" v-else>上传<i class="el-icon-upload el-icon--right"></i></el-button>
        </el-upload>
      </el-form-item>

      <el-form-item label="原价" prop="originalPrice">
        <el-input v-model="path.originalPrice" style="width: 400px"/>
      </el-form-item>

      <el-form-item label="现价" prop="price">
        <el-input v-model="path.price" style="width: 400px"/>
      </el-form-item>

      <el-form-item label="描述" prop="description">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="path.description"
          style="width: 400px">
        </el-input>
      </el-form-item>

      <el-form-item label="是否首页显示" prop="description">
        <el-switch
          v-model="path.isTop"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>

      <el-form-item label="是否启用" prop="description">
        <template>
          <el-radio v-model="path.status" :label="0">关闭</el-radio>
          <el-radio v-model="path.status" :label="1">启用</el-radio>
        </template>

      </el-form-item>


      <el-form-item>
        <el-button @click="update('path')" type="primary">保存</el-button>
        <el-button @click="$router.go(-1) ">取消</el-button>
      </el-form-item>

    </el-form>

  </div>
</template>
<script>
  export default {
    data() {
      return {
        path: {
          cover:''
        },
        categoryList: [],
        BASE_URL: process.env.BASE_URL,//获取dev.env.js中的BASE_API
        rules: {
          name: [
            {required: true, message: '请输入标题', trigger: 'blur'},
            {min: 5, max: 255, message: '长度在 5 到 255 个字符', trigger: 'blur'}
          ],
          categoryId: [
            {required: true, message: '请选择分类', trigger: 'change'}
          ],

        }
      }
    },
    created() {
      let id = this.$route.params.id
      if (id) {
        this.getData(id)
      }
      this.getCategoryList()
    },
    methods: {
      getData(id) {
        this.api({
          url: `/path/getPath/${id}`,
          method: 'get',
        }).then(data => {
          this.path = data
        })
      },
      getCategoryList(){
        this.api({
          url: `/path/getCategoryList`,
          method: 'get',
        }).then(data => {
          this.categoryList = data
        })
      },
      handleAvatarSuccess(res, file) {
        console.log(res)
        this.path.cover = res.info
      },
      beforeAvatarUpload(file) {
        const isLt2M = file.size / 1024 / 1024 < 2
        const typeArr=['image/jpeg','image/png','image/jpg']
        const isImage=typeArr.find(x=>x===file.type);
        if (!isImage) {
          this.$message.error('不支持上传此文件类型!')
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!')
        }
        return isImage && isLt2M
      },
      update(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.api({
              url: "/path/saveOrUpdatePath",
              method: "post",
              data: this.path
            }).then(() => {
              this.$router.replace('/learningpath')
              return this.$message({
                type: 'success',
                message: '修改成功!'
              });
            })
          } else {
            return false;
          }
        });

      }
    }
  }
</script>
